package com.css.fxfzmhwz.common.utils.cluster;

import org.geotools.referencing.GeodeticCalculator;
import org.geotools.referencing.crs.DefaultGeographicCRS;

public class Point {

    private static final double EARTH_RADIUS = 6378.137;//地球半径,单位千米
    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }
    // 84坐标系构造GeodeticCalculator
    public static GeodeticCalculator geodeticCalculator = new GeodeticCalculator(DefaultGeographicCRS.WGS84);

    public double lng;
    public double lat;

    public Point () {
    }

    public Point (double lng, double lat) {
        this.lng = lng;
        this.lat = lat;
    }


    /**
     *
     * @param lat1 第一个纬度
     * @param lng1 第一个经度
     * @param lat2 第二个纬度
     * @param lng2 第二个经度
     * @return 两个经纬度的距离
     */
    public static double getDistance(double lat1, double lng1,double lat2,double lng2) {
        /*
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);

        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
                Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        return s;*/
        // 起点经纬度
        /*geodeticCalculator.setStartingGeographicPoint(lng1, lat1);
        // 末点经纬度
        geodeticCalculator.setDestinationGeographicPoint(lng2, lat2);
        // 计算距离，单位：米
        double orthodromicDistance = geodeticCalculator.getOrthodromicDistance();
        return orthodromicDistance;*/
        return Math.sqrt(Math.pow((lng1 - lng2), 2) + Math.pow((lat1 - lat2), 2));
    }

    public double getDistance(Point point) {
        /*
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);

        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
                Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        return s;*/
        // 起点经纬度
        /*geodeticCalculator.setStartingGeographicPoint(lng1, lat1);
        // 末点经纬度
        geodeticCalculator.setDestinationGeographicPoint(lng2, lat2);
        // 计算距离，单位：米
        double orthodromicDistance = geodeticCalculator.getOrthodromicDistance();
        return orthodromicDistance;*/
        return Math.sqrt(Math.pow((this.lng - point.lng), 2) + Math.pow((this.lat - point.lat), 2));
    }

    @Override
    public boolean equals(Object obj) {
        if(!(obj instanceof Point)) {
            return false;
        }
        Point point = (Point) obj;
        // 地址相等
        if (this == point) {
            return true;
        }

        if (point.lng == this.lng && point.lat == this.lat) {
            return true;
        } else {
            return false;
        }
    }


    public static void main(String[] args) {
        //116.39027,39.92697 116.39121,39.92729
        long start = System.currentTimeMillis();
        System.out.println(getDistance(39.92697,116.39027 , 39.92729, 116.39121));
        long end = System.currentTimeMillis();
        System.out.println("计算距离耗时: " + ((end - start) / 1000.0) + "s");

    }

}
